<?php

/**
 * product actions.
 *
 * @package    sf_sandbox
 * @subpackage product
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class productActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeIndex()
    {
        return $this->redirect('product/productList');
    }

    public function executeProductList()
    {
        $this->cms_products = CmsProductPeer::doSelect(new Criteria());
    }

    public function executeProductCreate()
    {
        $this->cms_product = new CmsProduct();

        $this->setTemplate('productEdit');
    }

    public function executeProductEdit()
    {
        $this->cms_product = CmsProductPeer::retrieveByPk($this->getRequestParameter('product_id'));
        $this->forward404Unless($this->cms_product);
    }

    public function executeProductUpdate()
    {
        if (!$this->getRequestParameter('product_id')) {
            $cms_product = new CmsProduct();
        }
        else
        {
            $cms_product = CmsProductPeer::retrieveByPk($this->getRequestParameter('product_id'));
            $this->forward404Unless($cms_product);
        }

        $cms_product->setCategoriesId($this->getRequestParameter('categories_id'));
        $cms_product->setProductCode($this->getRequestParameter('product_code'));
        $cms_product->setProductName($this->getRequestParameter('product_name'));
        $cms_product->setProductDesc($this->getRequestParameter('product_desc'));
        $cms_product->setProductShortDesc($this->getRequestParameter('product_short_desc'));
        $cms_product->setProductNameCn($this->getRequestParameter('product_name_cn'));
        $cms_product->setProductDescCn($this->getRequestParameter('product_desc_cn'));
        $cms_product->setProductShortDescCn($this->getRequestParameter('product_short_desc_cn'));
        //$cms_product->setProductPhoto($this->getRequestParameter('product_photo'));
        $cms_product->setPrice($this->getRequestParameter('price'));
        $cms_product->setPrice2($this->getRequestParameter('price2'));
        $cms_product->setTotalUnit($this->getRequestParameter('total_unit'));
        $cms_product->setRate($this->getRequestParameter('rate'));
        //$cms_product->setModal($this->getRequestParameter('modal'));
        //$cms_product->setWeight($this->getRequestParameter('weight'));
        //$cms_product->setUom($this->getRequestParameter('uom'));
        $cms_product->setSpecialItem($this->getRequestParameter('special_item'));
        $cms_product->setStatusCode($this->getRequestParameter('status_code'));
        $cms_product->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));
        $cms_product->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));

        $cms_product->save();

        if ($this->getRequest()->getFileName('fileNew') != '') {
            //exit();
            $uploadedFilename = $this->getRequest()->getFileName('fileNew');
            $ext = explode(".", $this->getRequest()->getFileName('fileNew'));
            $extensionName = $ext[count($ext) - 1];
            //$thumbFile = time().rand(1000,9999).".".$ext[count($ext)-1];
            $filename = $cms_product->getProductId() . "." . $extensionName;
            $thumbFile = $cms_product->getProductId() . "." . $extensionName;

            $this->getRequest()->moveFile('fileNew', sfConfig::get('sf_upload_dir') . '/products/' . $filename);
            list($w, $h) = getimagesize('uploads/products/' . $filename);

            $new_height = 177;
            $new_width = $this->getImageResizeWidth($w, $h, $new_height);
            $new_img = imagecreatetruecolor($new_width, $new_height);
            //$img = imagecreatefromjpeg('uploads/products/'.$filename);
            $file_path = 'uploads/products/thumbnails/';
            $ori_file_path = 'uploads/products/';
            switch (strtolower(substr(strrchr($uploadedFilename, '.'), 1))) {
                case 'jpg':
                case 'jpeg':

                    $src_img = @imagecreatefromjpeg($ori_file_path . $filename);
                    $write_image = 'imagejpeg';
                    $image_quality = isset($options['jpeg_quality']) ?
                            $options['jpeg_quality'] : 75;

                    imagejpeg($new_img, "uploads/products/thumbnails/" . $thumbFile, 100);
                    break;
                case 'gif':
                    @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                    $src_img = @imagecreatefromgif($ori_file_path . $filename);
                    $write_image = 'imagegif';
                    $image_quality = null;
                    imagegif($new_img, "uploads/products/thumbnails/" . $thumbFile, 100);
                    break;
                case 'png':
                    @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                    @imagealphablending($new_img, false);
                    @imagesavealpha($new_img, true);
                    $src_img = @imagecreatefrompng($ori_file_path . $filename);
                    $write_image = 'imagepng';
                    $image_quality = isset($options['png_quality']) ?
                            $options['png_quality'] : 9;
                    imagepng($new_img, "uploads/products/thumbnails/" . $thumbFile, 100);
                    break;
                default:
                    $src_img = null;
            }
            $success = $src_img && @imagecopyresampled(
                                        $new_img,
                                        $src_img,
                                        0, 0, 0, 0,
                                        $new_width,
                                        $new_height,
                                        $w,
                                        $h
                                    ) && $write_image($new_img, $file_path . $thumbFile, $image_quality);
            imagedestroy($new_img);
            imagedestroy($src_img);

            $cms_product->setProductPhoto($thumbFile);
            $cms_product->save();
        }

        for ($i = 1; $i <= 5; $i++) {
            if ($this->getRequest()->getFileName('fileNew_' . $i) != '') {
                //exit();
                $uploadedFilename = $this->getRequest()->getFileName('fileNew_' . $i);
                $ext = explode(".", $this->getRequest()->getFileName('fileNew_' . $i));
                $extensionName = $ext[count($ext) - 1];
                //$thumbFile = time().rand(1000,9999).".".$ext[count($ext)-1];
                $filename = $cms_product->getProductId() . "-$i." . $extensionName;
                $thumbFile = $cms_product->getProductId() . "-$i." . $extensionName;

                $this->getRequest()->moveFile('fileNew_' . $i, sfConfig::get('sf_upload_dir') . '/products/' . $filename);
                list($w, $h) = getimagesize('uploads/products/' . $filename);

                $new_height = 177;
                $new_width = $this->getImageResizeWidth($w, $h, $new_height);
                $new_img = imagecreatetruecolor($new_width, $new_height);
                //$img = imagecreatefromjpeg('uploads/products/'.$filename);
                $file_path = 'uploads/products/thumbnails/';
                $ori_file_path = 'uploads/products/';
                switch (strtolower(substr(strrchr($uploadedFilename, '.'), 1))) {
                    case 'jpg':
                    case 'jpeg':

                        $src_img = @imagecreatefromjpeg($ori_file_path . $filename);
                        $write_image = 'imagejpeg';
                        $image_quality = isset($options['jpeg_quality']) ?
                                $options['jpeg_quality'] : 75;

                        imagejpeg($new_img, "uploads/products/thumbnails/" . $thumbFile, 100);
                        break;
                    case 'gif':
                        @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                        $src_img = @imagecreatefromgif($ori_file_path . $filename);
                        $write_image = 'imagegif';
                        $image_quality = null;
                        imagegif($new_img, "uploads/products/thumbnails/" . $thumbFile, 100);
                        break;
                    case 'png':
                        @imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
                        @imagealphablending($new_img, false);
                        @imagesavealpha($new_img, true);
                        $src_img = @imagecreatefrompng($ori_file_path . $filename);
                        $write_image = 'imagepng';
                        $image_quality = isset($options['png_quality']) ?
                                $options['png_quality'] : 9;
                        imagepng($new_img, "uploads/products/thumbnails/" . $thumbFile, 100);
                        break;
                    default:
                        $src_img = null;
                }
                $success = $src_img && @imagecopyresampled(
                                            $new_img,
                                            $src_img,
                                            0, 0, 0, 0,
                                            $new_width,
                                            $new_height,
                                            $w,
                                            $h
                                        ) && $write_image($new_img, $file_path . $thumbFile, $image_quality);
                imagedestroy($new_img);
                imagedestroy($src_img);

                $c = new Criteria();
                $c->add(CmsProductImagePeer::PRODUCT_ID, $cms_product->getProductId());
                $c->add(CmsProductImagePeer::SEQ, $i);
                $cms_product_image = CmsProductImagePeer::doSelectOne($c);

                if (!$cms_product_image) {
                    $cms_product_image = new CmsProductImage();
                }
                $cms_product_image->setProductId($cms_product->getProductId());
                $cms_product_image->setSeq($i);
//                $cms_product_image->setImageWidth($this->getRequestParameter('image_width'));
//                $cms_product_image->setImageHeight($this->getRequestParameter('image_height'));
//                $cms_product_image->setImageTitle($this->getRequestParameter('image_title'));
//                $cms_product_image->setImageAlt($this->getRequestParameter('image_alt'));
                $cms_product_image->setImageSrc($thumbFile);
                $cms_product_image->setStatusCode(Globals::STATUS_ACTIVE);
                $cms_product_image->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));
                $cms_product_image->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID));

                $cms_product_image->save();
            }
        }

        $this->setFlash('successMsg', "Update successfully");
        return $this->redirect('product/productEdit?product_id=' . $cms_product->getProductId());
    }

    function getImageResizeWidth($width, $height, $maxHeight)
    {
        $percentage = ($height / $maxHeight);

        $width = round($width / $percentage);

        return $width;
    }
}
